// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-24 12:30
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.leetCode.EqualSubstring;

public class EqualSubstring {
    public int equalSubstring(String s, String t, int maxCost) {
        // Try Your Best To Use Lest Source To Make Two String Equality
        // Make s - t that get the absolute value,than according the values to commute the maximum number
        // The Length of Two String is equals
        // SubString->Sliding Window
        int len = s.length();
        int[] dis = new int[len];
        for (int i = 0; i < len; i++) {
            dis[i] = Math.abs(s.charAt(i) - t.charAt(i));
        }
        int L = 0, R = 0;
        int cost = 0;
        int max = 0;
        while (R < len) {
            cost += dis[R++];
            while (cost > maxCost) {
                cost -= dis[L++];
            }
            max = Math.max(max, R - L);
        }
        return max;
    }
}
